<template>
	<div>
		<el-form :model="configData" label-width="120px" >
			<div v-for="(item, index) in inputForm" :key="index">
				<el-form-item :prop="reservePoint(item.config_value)" :label="item.config_name">
					<el-input class="w-ipt" v-model="item.config_value" :placeholder="'请输入' + item.config_name">
					</el-input>
				</el-form-item>
			</div>
			<div v-for="(item, index) in selectForm" :key="index">
				<el-form-item :prop="item.config_value" :label="item.config_name">
					<el-select v-model="item.config_value" :placeholder="'请选择' + item.config_name">
						<el-option v-for="(value, name) in item.optional_value" :key="name" :label="value"
							:value="value"></el-option>
					</el-select>
				</el-form-item>
			</div>
			<el-form-item>
				<el-button type="primary" @click="submitConfig">立即配置</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import API from '@/api/index.js'

	export default {
		data() {
			
			return {
				requestParma: {
					device_id: '',
					device_type: ''
				},
				configData: [],
				form: {},
			}
		},
		created() {
			this.requestParma.device_id = localStorage.getItem('device_id');
			this.requestParma.device_type = localStorage.getItem('device_type');
			this.getConfigs();
		},
		computed: {
			inputForm() {
				return this.configData.filter(item => {
					return item.input_type === 'input';
				});
			},
			selectForm() {
				return this.configData.filter(item => {
					return item.input_type === 'select';
				});
			}
		},
		methods: {
			reservePoint(val) {
				console.log(val)
			},
			getConfigs() {
				let self = this;
				API.deviceConfig(self.requestParma.device_id, self.requestParma.device_type).then(res => {
					self.configData = res.data.data;
				});
			},
			submitConfig() {
				let self = this;
				self.form = {
					device_id: localStorage.getItem('device_id'),
					configs: self.configData
				}
				console.log(self.form)
				API.deviceConfigs(self.form).then(res => {
					console.log(res)
					self.$message.success("配置成功");
					self.getConfigs();
				})
			}
		}
	}
</script>

<style>
</style>
